package tables

import (
	"fmt"

	"github.com/astaxie/beego/orm"
)

type Config struct {
	Id    int
	Key   string `orm:"size(20);unique;index"`
	Value string `orm:"size(50)"`
}

const (
	Os            = "1"
	Artiture      = "2"
	Linetool_type = "3"
	Svn_place     = "4"
)

func (c *Config) TableName() string {
	return "config"
}

//保存系统信息
func (c *Config) SaveSystemInfo(os string, artiture string) {
	o := orm.NewOrm()
	o.Delete(&Config{Key: Artiture})
	o.Delete(&Config{Key: Os})
	config_arr := []Config{
		{Key: Os, Value: os},
		{Key: Artiture, Value: artiture},
	}
	o.InsertMulti(100, config_arr)
}

//保存svn路径
func (c *Config) SaveSvnPlace(address string) {
	o := orm.NewOrm()
	config_arr := []Config{
		{Key: Linetool_type, Value: "svn"},
		{Key: Svn_place, Value: address},
	}
	o.InsertMulti(100, config_arr)
}

func (c *Config) GetByKeys(keys []string) map[string]string {
	cond := orm.NewCondition()
	o := orm.NewOrm()

	for _, key := range keys {
		cond = cond.Or("key", key)
	}
	qs := o.QueryTable(c)
	var cfgs []*Config
	qs.SetCond(cond).All(&cfgs)

	result := make(map[string]string)
	fmt.Println(len(cfgs))
	for _, cfg := range cfgs {
		fmt.Println(cfg.Key)
		fmt.Println(cfg.Value)
		result[cfg.Key] = cfg.Value
	}
	return result
}
